/*
 * Copyright 2012 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package test.fox.servlet;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

/**
 * 类EncodingFilter.java的实现描述：TODO 类实现描述
 * 
 * @author husong 2012-10-27 下午7:41:26
 */
@SuppressWarnings("serial")
public class EncodingFilter extends HttpServlet implements Filter {

    private FilterConfig filterConfig;
    private String       trargetEncoding = null;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        this.trargetEncoding = this.filterConfig.getInitParameter("encoding");

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) {
        // TODO Auto-generated method stub
        try {
            // 在控制台输出目标编码
            System.out.println("trargetEncoding:" + trargetEncoding + "\n");
            // 执行编码，实际的处理内容
            request.setCharacterEncoding(trargetEncoding);
            filterChain.doFilter(request, response);
        } catch (ServletException sx) {
            filterConfig.getServletContext().log(sx.getMessage());

        } catch (IOException iox) {
            filterConfig.getServletContext().log(iox.getMessage());
        }
    }

    @Override
    public void destroy() {
        // 清空资源
        this.filterConfig = null;
        this.trargetEncoding = null;

    }
}
